

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-blog/img/fluid.png">
  <link rel="icon" href="/my-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="木舟">
  <meta name="keywords" content="python Vue Test 测试开发 自动化测试">
  
    <meta name="description" content="前言从事互联网行业，尤其是研发和测试，想必都听过docker的鼎鼎大名，它是互联网快速交付需求下的产物。 在熟悉docker的镜像配置后，可以让开发者们轻松、快速的打包应用程序，部署到一个轻量、低耗、可移植的容器中。 docker容器构建的环境是独立的、干净的，可以确保应用程序的运行不受外界环境的影响，他就是一个沙箱。 在目前持续集成（CICD）的趋势下，docker已经逐渐成为开发，乃至测试都需">
<meta property="og:type" content="article">
<meta property="og:title" content="docker -- 持续集成必备工具">
<meta property="og:url" content="https://walker_kwu.gitee.io/my-blog/2022/10/17/devops/docker/">
<meta property="og:site_name" content="My Blog">
<meta property="og:description" content="前言从事互联网行业，尤其是研发和测试，想必都听过docker的鼎鼎大名，它是互联网快速交付需求下的产物。 在熟悉docker的镜像配置后，可以让开发者们轻松、快速的打包应用程序，部署到一个轻量、低耗、可移植的容器中。 docker容器构建的环境是独立的、干净的，可以确保应用程序的运行不受外界环境的影响，他就是一个沙箱。 在目前持续集成（CICD）的趋势下，docker已经逐渐成为开发，乃至测试都需">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://walker_kwu.gitee.io/my-blog/img/use/dokcer%E9%95%9C%E5%83%8F.png">
<meta property="og:image" content="https://walker_kwu.gitee.io/my-blog/img/use/docker%E5%AE%B9%E5%99%A8.png">
<meta property="article:published_time" content="2022-10-17T05:52:54.000Z">
<meta property="article:modified_time" content="2023-05-23T10:37:53.246Z">
<meta property="article:author" content="木舟">
<meta property="article:tag" content="测试开发">
<meta property="article:tag" content="持续集成">
<meta property="article:tag" content="dokcer">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://walker_kwu.gitee.io/my-blog/img/use/dokcer%E9%95%9C%E5%83%8F.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>docker -- 持续集成必备工具 - My Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"walker_kwu.gitee.io","root":"/my-blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-blog/js/utils.js" ></script>
  <script  src="/my-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-blog/">
      <strong>木舟</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="docker -- 持续集成必备工具"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        木舟
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-10-17 13:52" pubdate>
          2022年10月17日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4.9k 字
        
      </span>
    

    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">docker -- 持续集成必备工具</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月23日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>从事互联网行业，尤其是研发和测试，想必都听过docker的鼎鼎大名，它是互联网<strong>快速交付</strong>需求下的产物。</p>
<p>在熟悉docker的镜像配置后，可以让开发者们轻松、快速的打包应用程序，部署到一个轻量、低耗、可移植的容器中。</p>
<p>docker容器构建的环境是独立的、干净的，可以确保应用程序的运行不受外界环境的影响，他就是一个<strong>沙箱</strong>。</p>
<p>在目前<strong>持续集成（CICD）</strong>的趋势下，docker已经逐渐成为开发，乃至测试都需要掌握的知识。</p>
<p>docker有三大核心知识点：</p>
<ul>
<li>仓库</li>
<li>镜像</li>
<li>容器</li>
</ul>
<p>下面我们就围绕三个核心知识，来进行讲解。</p>
<blockquote>
<p>docker的安装，需要自行百度教程进行安装</p>
</blockquote>
<h1 id="仓库"><a href="#仓库" class="headerlink" title="仓库"></a>仓库</h1><p>仓库是存放<strong>镜像</strong>的地方, 供使用者拉取。</p>
<p>镜像拉取后，会在本地存放一个独一无二的镜像文件。</p>
<p>运行<code>docker images</code>可以看到当前本地下的所有镜像。</p>
<p>一般有几个关键字段用来区分：</p>
<ul>
<li>镜像id，随机id，不会重复</li>
<li>镜像名称，可重复</li>
<li>镜像标签，一般用来作为版本区分，同名称下标签不可重复</li>
</ul>
<p>docker安装时的默认仓库为：Dokcer Hub</p>
<p>目前 Docker 官方维护的一个公共仓库，大部分需求我们都可以从 Docker Hub 中直接下载镜像来实现。</p>
<p>比如：</p>
<ul>
<li>python</li>
<li>nginx</li>
<li>jenkins</li>
</ul>
<p>仓库还分为公共仓库和私人仓库，不过不在本章的重点，就不展开说明了。</p>
<p>一般情况下，如果不考虑代码安全，直接使用公共仓库就可以了。</p>
<h1 id="镜像"><a href="#镜像" class="headerlink" title="镜像"></a>镜像</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>仓库存放的是镜像，那么镜像是什么？</p>
<p>镜像就是一个模板，你可以把它看成一个<strong>类</strong>。</p>
<p>通过镜像我们可以构造出<strong>容器</strong>，相当于根据模板创造了某件东西。</p>
<p>比如说：</p>
<blockquote>
<p>工作餐的标配是：一碗米饭，一份土豆丝，一份辣椒炒肉。<br>这个标配可以看作一个镜像，然后通过这个标配镜像，把工作餐分发给公司的员工。<br>每个员工到手的就是一个容器，就是具体的工作餐。<br>员工可以根据现在的工作餐来进行增减，比如加个鸡腿，或者有人不爱吃土豆丝，可以去掉。<br>这个增减操作，就是对构建后的容器的二次命令。</p>
</blockquote>
<p>那么，如何构建镜像？</p>
<h2 id="直接从仓库中拉取"><a href="#直接从仓库中拉取" class="headerlink" title="直接从仓库中拉取"></a>直接从仓库中拉取</h2><p>通过命令<code>docker pull xxx</code></p>
<p>来进行镜像的拉取，比如<code>docker pull nginx:latest</code></p>
<p>表示：从仓库中拉取一个最新版本的nginx镜像，冒号后面是版本号，latest表示最新。</p>
<p>拉取后运行<code>docker images</code>，查看拉取的镜像。</p>
<p><img src="/my-blog/img/use/dokcer%E9%95%9C%E5%83%8F.png" srcset="/my-blog/img/loading.gif" lazyload></p>
<p>我们只需要关注：</p>
<ul>
<li>repository，镜像名称</li>
<li>tag，标签，也可以叫做版本</li>
<li>image id，镜像id</li>
</ul>
<h2 id="Dockerfile"><a href="#Dockerfile" class="headerlink" title="Dockerfile"></a>Dockerfile</h2><p>有时候，直接通过```docker pull``拉取的镜像，已经不满足部署需求。</p>
<p>比如python的web应用，需要：</p>
<ul>
<li>python</li>
<li>需要用到的三方库的安装</li>
<li>进程管理工具</li>
</ul>
<p>这时候，如果我们直接通过<code>docker pull python:latest</code>是满足不了环境的要求的。</p>
<p>所以这时候需要定制构建镜像的方式来生成镜像，这就需要用到<strong>Dockerfile</strong></p>
<p>Dockerfile放在下面讲。</p>
<h1 id="容器"><a href="#容器" class="headerlink" title="容器"></a>容器</h1><p>容器就是一个实例，一个对象，一个可以运行应用程序的环境。</p>
<p>可以通过<code>docker ps -a</code>来查看所有的容器。</p>
<p><img src="/my-blog/img/use/docker%E5%AE%B9%E5%99%A8.png" srcset="/my-blog/img/loading.gif" lazyload></p>
<ul>
<li>container id，容器id</li>
<li>image，是从哪个镜像构建的</li>
<li>command，容器生成后的运行命令</li>
<li>status，运行状态，up xxx为正常运行的状态</li>
<li>port，端口映射，左边是本地端口，右边是容器端口</li>
<li>names，容器名称</li>
</ul>
<p>那么，如何构建一个容器？</p>
<h2 id="docker-run"><a href="#docker-run" class="headerlink" title="docker run"></a>docker run</h2><p><code>docker run --name=&quot;yourContainerName&quot; -p 8000:80 -d nginx</code><br><code>docker run --name=&quot;yourContainerName&quot; -p 8000:80 -d nginx:latest</code></p>
<p>参数说明：</p>
<ul>
<li>–name，指定容器名称</li>
<li>-p，指定映射端口，左边是本机端口，右边是容器端口<ul>
<li>nginx运行时，默认开辟的端口是80和443<ul>
<li>如果是http协议，就是80端口</li>
<li>如果是https协议，就是443端口</li>
</ul>
</li>
</ul>
</li>
<li>-d，表示容器生成后不会进入容器</li>
<li>最后一个参数直接写具体的镜像，加冒号可以指定版本</li>
</ul>
<h2 id="docker-compose"><a href="#docker-compose" class="headerlink" title="docker-compose"></a>docker-compose</h2><p>docker run 只能适用于简单的构建。</p>
<p>实际上，我们的容器还需要指定其他的参数，比如数据挂载。</p>
<p>关于数据挂载：</p>
<blockquote>
<p>容器是单独的一个环境，里面产生的数据会随着容器的销毁而销毁<br>但是我们在容器产生的数据是需要保留的，比如：数据库的数据、运行日志<br>所以这时候我们需要通过数据挂载的方式，进行数据同步。<br>容器产生的数据，实际能够保存到本地。</p>
</blockquote>
<p>docker-compose需要进行额外的安装，可以自行百度如何安装。</p>
<p>实际上，我更建议使用docker-compose。</p>
<p>如何使用，放在下面进行说明。</p>
<h1 id="基础使用"><a href="#基础使用" class="headerlink" title="基础使用"></a>基础使用</h1><h2 id="构建镜像"><a href="#构建镜像" class="headerlink" title="构建镜像"></a>构建镜像</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-comment"># 先查看一下本地的镜像</span><br><span class="hljs-comment"># 这里我想看看本地有没有nginx</span><br><br>docker images<br><br><span class="hljs-comment"># 如果没有，则需要进行拉取</span><br><span class="hljs-comment"># 直接拉取最新版本</span><br>docker pull nginx:latest<br><br><span class="hljs-comment"># 如果有不需要的镜像，可以通过docker rmi 来删除镜像</span><br><span class="hljs-comment"># 注意，删除镜像前，需要确定是否用容器在使用镜像，docker ps -a</span><br><br>docker rmi 镜像名称<br>docker rmi 镜像<span class="hljs-built_in">id</span><br><br></code></pre></td></tr></table></figure>

<h2 id="通过镜像进行构建容器"><a href="#通过镜像进行构建容器" class="headerlink" title="通过镜像进行构建容器"></a>通过镜像进行构建容器</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-comment"># 先查看一下目前运行的容器的运行情况，再来确定容器的名字</span><br><br>docker ps -a<br><br><span class="hljs-comment"># 如果发现有不需要的容器可以进行删除</span><br><br><span class="hljs-comment"># 如果正在运行，需要先停止</span><br><br>docker stop 容器名称<br>docker stop 容器<span class="hljs-built_in">id</span><br><br><span class="hljs-comment"># 停止后再删除</span><br><br>docker <span class="hljs-built_in">rm</span> 容器名称<br>docker <span class="hljs-built_in">rm</span> 容器<span class="hljs-built_in">id</span><br><br><span class="hljs-comment"># 构建容器</span><br>docker run --name=<span class="hljs-string">&quot;yourContainerName&quot;</span> -p 8000:80 -d nginx:latest<br></code></pre></td></tr></table></figure>

<h2 id="进入容器"><a href="#进入容器" class="headerlink" title="进入容器"></a>进入容器</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-comment"># 进入容器前，需要查看当前的容器，来确定如何进入</span><br><br>docker ps -a<br><br><span class="hljs-comment"># 确定容器id、容器名称</span><br><span class="hljs-comment"># 然后通过exec命令进入容器</span><br><span class="hljs-comment"># 注意，此时容器应该处于up状态，不然无法进入</span><br><br>docker <span class="hljs-built_in">exec</span> -it 容器名称 /bin/bash<br>docker <span class="hljs-built_in">exec</span> -it 容器<span class="hljs-built_in">id</span> /bin/bash<br><br><span class="hljs-comment"># 运行时，比如指定一个运行终端的bash，通常为/bin/bash</span><br><br><span class="hljs-comment"># 如果想退出容器，只需要输入 exit 回车</span><br></code></pre></td></tr></table></figure>

<h1 id="docker-compose-1"><a href="#docker-compose-1" class="headerlink" title="docker-compose"></a>docker-compose</h1><p>docker-compose是基于配置文件来构建容器的一种工具。</p>
<p>相比于<code>docker run</code>，采用yaml配置文件，字段更加清晰可控。</p>
<p>并且，docker-compose支持一次性启动多个容器。</p>
<h2 id="配置实例"><a href="#配置实例" class="headerlink" title="配置实例"></a>配置实例</h2><p>新建docker-compose.yaml</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">version:</span> <span class="hljs-string">&#x27;2.4&#x27;</span>  <span class="hljs-comment"># docker-compose的版本</span><br><span class="hljs-attr">services:</span>  <span class="hljs-comment"># 需要运行的容器字典集合</span><br>    <span class="hljs-attr">nginxTest:</span> <br>        <span class="hljs-attr">image:</span> <span class="hljs-string">nginx</span>  <span class="hljs-comment"># 需要构建的镜像模板</span><br>        <span class="hljs-attr">restart:</span> <span class="hljs-string">always</span>  <span class="hljs-comment"># 是否需要重启</span><br>        <span class="hljs-attr">container_name:</span> <span class="hljs-string">nginxTest</span>  <span class="hljs-comment"># 构建的容器名称</span><br>        <span class="hljs-attr">environment:</span> <br>            <span class="hljs-bullet">-</span> <span class="hljs-string">TZ=Asia/Shanghai</span>  <span class="hljs-comment"># 配置市区</span><br>        <span class="hljs-attr">ports:</span><br>            <span class="hljs-bullet">-</span> <span class="hljs-number">8000</span><span class="hljs-string">:80</span>  <span class="hljs-comment"># 端口映射，左边是本机端口，右边是容器端口</span><br>        <span class="hljs-attr">volumes:</span>  <span class="hljs-comment"># 数据挂载，左边是本地路径，右边是容器路径</span><br>            <span class="hljs-bullet">-</span> <span class="hljs-string">/home/nginx/conf:/etc/nginx/conf.d</span><br>            <span class="hljs-bullet">-</span> <span class="hljs-string">/home/nginx/logs:/var/log/nginx</span><br>            <span class="hljs-bullet">-</span> <span class="hljs-string">/home/nginx/nginx.conf:/etc/nginx/nginx.conf</span><br>            <span class="hljs-bullet">-</span> <span class="hljs-string">/home/nginx/html:/usr/share/nginx/html</span><br><br>    <span class="hljs-attr">mysqlTest:</span><br>        <span class="hljs-attr">image:</span> <span class="hljs-string">mysql</span><br>        <span class="hljs-attr">restart:</span> <span class="hljs-string">always</span><br>        <span class="hljs-attr">container_name:</span> <span class="hljs-string">mysqlTest</span><br>        <span class="hljs-attr">environment:</span><br>            <span class="hljs-attr">MYSQL_ROOT_PASSWORD:</span> <span class="hljs-string">&#x27;1qaz2wsx3edc&#x27;</span>  <span class="hljs-comment"># 定义root的默认密码</span><br>            <span class="hljs-attr">TZ:</span> <span class="hljs-string">Asia/Shanghai</span><br>        <span class="hljs-attr">ports:</span><br>            <span class="hljs-bullet">-</span> <span class="hljs-number">33061</span><span class="hljs-string">:3306</span><br>        <span class="hljs-attr">volumes:</span><br>            <span class="hljs-bullet">-</span> <span class="hljs-string">/home/mysql/data:/var/lib/mysql</span>  <span class="hljs-comment"># 将容器中数据库产生的数据挂载到本地，容器销毁后，数据仍存在。这样可以保证数据库运行环境干净，且数据持久化</span><br>            <span class="hljs-bullet">-</span> <span class="hljs-string">/home/mysql/conf/my.cnf:/etc/my.cnf</span>  <span class="hljs-comment"># mysql初始化配置</span><br></code></pre></td></tr></table></figure>

<h2 id="如何运行"><a href="#如何运行" class="headerlink" title="如何运行"></a>如何运行</h2><p>通过命令<code>docker-compose up -d</code>来运行</p>
<p>通过参数<code>-d</code>，可以让容器后台运行。</p>
<p>如果不加<code>-d</code>参数，一般用于调试，退出后，容器也会停止。</p>
<p>运行时，需要先切换到docker-compose.yaml配置文件所在的目录，然后直接运行<code>docker-compose up -d</code></p>
<h1 id="Dockerfile-1"><a href="#Dockerfile-1" class="headerlink" title="Dockerfile"></a>Dockerfile</h1><p>Dockerfile需要配合命令<code>docker build -t xxx:xxx</code>来使用，这个后续会进行说明。</p>
<p>Dockerfile最主要的几个命令：</p>
<ul>
<li>FROM，基础镜像。意思就是你需要将哪个镜像作为基础来进行自定义修改</li>
<li>COPY，将本机文件拷贝到镜像路径，相对路径基于docker build的指定路径</li>
<li>RUN，在镜像模板构建时运行的命令</li>
<li>WORKDIR，镜像工作目录，RUN命令的相对路径基于WORKDIR</li>
<li>EXPOSE，对外端口</li>
<li>CMD，镜像被构建出容器后的运行命令</li>
</ul>
<p>Dockerfile由上到下依次运行。</p>
<h2 id="Dockerfile举例"><a href="#Dockerfile举例" class="headerlink" title="Dockerfile举例"></a>Dockerfile举例</h2><p>新建Dockerfile，注意Dockerfile文件，开头的<strong>D</strong>要大写</p>
<figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs Dockerfile"><span class="hljs-keyword">FROM</span> python:<span class="hljs-number">3.9</span>  <span class="hljs-comment"># 将python3.9作为基础镜像</span><br><br><span class="hljs-keyword">RUN</span><span class="language-bash"> <span class="hljs-built_in">mkdir</span> /Work  <span class="hljs-comment"># 在镜像中创建文件夹Work</span></span><br><br><span class="hljs-keyword">COPY</span><span class="language-bash"> ./myProjct /Work/myProject  <span class="hljs-comment"># 拷贝文件</span></span><br><br><span class="hljs-keyword">WORKDIR</span><span class="language-bash"> /Work  <span class="hljs-comment"># 规定镜像的工作目录</span></span><br><br><span class="hljs-keyword">RUN</span><span class="language-bash"> pip3 install -r ./myProject/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple  <span class="hljs-comment"># 安装项目环境第三方库</span></span><br><br><span class="hljs-keyword">EXPOSE</span> <span class="hljs-number">8000</span>  <span class="hljs-comment"># 映射8000端口</span><br><br><span class="hljs-keyword">CMD</span><span class="language-bash"> [<span class="hljs-string">&quot;python3&quot;</span>, <span class="hljs-string">&quot;/Work/myProject/main.py&quot;</span>]</span><br></code></pre></td></tr></table></figure>

<p>可以看出，如果没有Dockerfile，我们只能得到一个python3.9的镜像。</p>
<p>而得不到一个拥有完美运行你的python程序的环境，你得先去构建容器才能安装第三方库。</p>
<p>这显然是不符合<strong>持续集成</strong>标准。</p>
<h2 id="如何运行-1"><a href="#如何运行-1" class="headerlink" title="如何运行"></a>如何运行</h2><p>切换路径到Dockerfile文件存放路径</p>
<p>运行<code>docker build -t pythonExe:latest .</code></p>
<p>运行前需要先保证<strong>没有同名且同版本</strong>的镜像</p>
<h1 id="docker常用命令"><a href="#docker常用命令" class="headerlink" title="docker常用命令"></a>docker常用命令</h1><ul>
<li>docker pull, 拉取的镜像</li>
<li>docker images，查看本地的镜像</li>
<li>docker search，搜索仓库中的镜像</li>
<li>docker run，运行一个容器</li>
<li>docker rmi，删除镜像</li>
<li>docker exec，进入一个容器，或向容器发送执行命令</li>
<li>docker stop，停止容器</li>
<li>docker start，启动容器</li>
<li>docker restart，重启容器</li>
<li>docker rm，删除容器</li>
<li>docker ps，查看运行的容器，加参数<code>-a</code>，查看所有的容器，包括未运行的</li>
<li>docker build，根据Dockerfile构建镜像</li>
</ul>
<h1 id="持续集成流程"><a href="#持续集成流程" class="headerlink" title="持续集成流程"></a>持续集成流程</h1><p>docker的优秀机制，和持续集成真的十分契合，目前持续集成也或多或少会出现在面试当中，掌握其流程还是很有必要的。</p>
<p>目前持续集成的流程架构一般为：</p>
<p>git + jenkins + docker + 进程管理工具 + 自动化测试</p>
<p>下面就来介绍一下持续集成大体的流程：</p>
<ul>
<li>在jenkins中新建任务，监听git仓库的特定分支，比如master</li>
<li>在特定分支进行代码更新时候，触发jenkins构建</li>
<li>jenkins构建中配置好服务器部署流程，有直接用sh的，也有直接用pipeline的。</li>
<li>部署流程中，必不可少的会用到docker来进行部署</li>
<li>在docker中，通过Dockerfile进行环境配置，包括进程管理工具的安装、程序运行环境配置，以及最后在CMD中编写启动服务的命令。</li>
<li>然后会根据是测试环境代码还是生产环境代码来判断是否需要进行自动化测试</li>
</ul>
<p>自动化测试可以配置在<strong>构建后步骤</strong>。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-blog/categories/%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90/" class="category-chain-item">持续集成</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/">#测试开发</a>
      
        <a href="/my-blog/tags/%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90/">#持续集成</a>
      
        <a href="/my-blog/tags/dokcer/">#dokcer</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>docker -- 持续集成必备工具</div>
      <div>https://walker_kwu.gitee.io/my-blog/2022/10/17/devops/docker/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>木舟</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年10月17日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2023年5月23日</div>
        </div>
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="NC - 非商业性使用">
                <i class="iconfont icon-nc"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="SA - 相同方式共享">
                <i class="iconfont icon-sa"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-blog/2022/10/22/notes/js/this/" title="this精讲">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">this精讲</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-blog/2022/10/09/notes/js/object/" title="对象精讲">
                        <span class="hidden-mobile">对象精讲</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>By 木舟</span> <i class="iconfont icon-love"></i> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-blog/js/events.js" ></script>
<script  src="/my-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
